package com.xf.micro.core.filter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ServiceLoader;

/**
 * @author zouxf
 * @date 2023/12/26 17:02
 * @description
 */
public class FilterChainFactory {

    private static List<CoreFilter> filters = new ArrayList<>();

    static {
        loadFilters();
    }

    private static void loadFilters() {
        ServiceLoader<CoreFilter> coreFilters = ServiceLoader.load(CoreFilter.class);
        Iterator<CoreFilter> iterator = coreFilters.iterator();
        while (iterator.hasNext()) {
            CoreFilter coreFilter = iterator.next();
            filters.add(coreFilter);
        }
    }

    public static FilterChain getFilterChain() {
        GlobalFilterChain filterChain = new GlobalFilterChain();
        filterChain.addPreFilters(filters);
        return filterChain;
    }
}
